﻿//服务代理类
var ServiceProxy = function () {

    //中药材网站基地址
    //var CONST_ZYCTD_SERVICE_BASE_URI = "http://www" + ZYCLib.Domain ;
    var CONST_ZYCTD_SERVICE_BASE_URI = "/";

    //url: "../User/Add",     
    //控制器Uri
    
    var CONST_INFO_GETINFOTOPICDATAS_URI = "Infomation/GetInfoTopicDatas";

    var CONST_PERMISSION_GETALLROLES_URI = "Permission/GetAllRoles";

    var CONST_PERMISSION_GETALLROLEITEMS_URI = "Permission/GetAllRoleItems";

    var CONST_PERMISSION_GETROLEAUTHORITY_URI = "Permission/GetRoleAuthority";

    var CONST_PERMISSION_GETROLEAUTHORITIES_URI = "Permission/GetRoleAuthorities";

    var CONST_PERMISSION_GETROLEUSERS_URI = "Permission/GetRoleUsers"; 
    
    var CONST_PERMISSION_EDITROLE_URI = "Permission/EditRole";

    var CONST_PERMISSION_REMOVEROLE_URI = "Permission/RemoveRole";

    var CONST_PERMISSION_EDITROLEAUTHORITY_URI = "Permission/EditRoleAuthority";

    var CONST_PERMISSION_REMOVEROLEAUTHORITY_URI = "Permission/RemoveRoleAuthority";

    var CONST_PERMISSION_ASSIGNBATCHUSERTOROLE_URI = "Permission/AssignBatchUserToRole";

    var CONST_PERMISSION_REMOVEBATCHUSERFROMROLE_URI = "Permission/RemoveBatchUserFromRole";

    var CONST_PERMISSION_SEARCHUSERS_URI = "Permission/SearchUsers";

    var CONST_INFO_GETINFOTOPICS_URI = "Info/GetInfoTopics"; 

    var CONST_INFO_GETINFOTOPICRANKINGS_URI = "Info/GetInfoTopicRankings";

    var CONST_INFO_GETNEWESTINFOTOPICS_URI = "Info/GetNewestInfoTopics";

    var CONST_INFO_GETSUBLETTERSBYLETTER_URI = "Info/GetSubLettersByLetter";

    var CONST_INFO_GETMCODEXSBYLETTER_URI = "Info/GetMCodexsByLetter";

    //资讯
    var CONST_INFO_GETINFOTOPICDETAIL_URI = "Info/GetInfoTopicDetail";
    var CONST_INFO_GETINFOTOPICREVIEWS_URI = "Info/GetInfoTopicReviews";
    var CONST_INFO_GETMBRELATIVEDTOPICS_URI = "Info/GetMBRelativedTopics";
    var CONST_INFO_GETMBRELATIVEDPOSTS_URI = "Info/GetMBRelativedPosts";
    var CONST_INFO_ADDINFOTOPICREVIEW_URI = "Info/AddInfoTopicReview";
    var CONST_INFO_GETMBRELATIVEDTOPICBASES_URI = "Info/GetMBRelativedTopicBases";
    var CONST_INFO_GETMBRELATIVEDTOPICITEMS_URI = "Info/GetMBRelativedTopicItems";
    var CONST_INFO_GETMBEXCLUDECATATOPICS_URI = "Info/GetMBExcludeCataTopics";

    //药材
    var CONST_MCODEX_GETMCODEXBASE_URI = "Breeds/GetMCodexBase";
    var CONST_MCODEX_GETMCODEXRECOMMENDBUSINESSES_URI = "Breeds/GetMCodexRecommendBusinesses";
    
    //价格
    var CONST_PRICE_GETMCODEXPRICES_URI = "Breeds/GetMCodexPrices";

    //验证码
    var CONST_VERIFYCODE_VALIDATECODE_URI = "VerifyCodeImage/ValidateCode";

    //用户中心
    var CONST_FAVOTITE_ADDFAVORITEITEM_URI = "Favorite/AddFavoriteItem";
    var CONST_FAVOTITE_REMOVEFAVOTITE_URI = "Favorite/RemoveFavoriteItem";
    var CONST_FAVOTITE_ISEXISTFAVORITEITEM_URI = "Favorite/IsExistFavoriteItem";

    //Field
    this._ServiceUri = CONST_ZYCTD_SERVICE_BASE_URI;

    //Funcion
    if (typeof ServiceProxy._initialized == "undefined") {

        //按分页获取资讯信息
        ServiceProxy.prototype.getInfoList = function (cataID, mbID, mbName, pcaID, pcaName, keyWord, pageIndex, pageSize, callback) {
            var apiUri = this._ServiceUri + CONST_INFO_GETINFOTOPICDATAS_URI + "?cataID=" + cataID + "&mbID=" + mbID + "&mbName=" + mbName + "&pcaID=" + pcaID + "&pcaName=" + pcaName + "&keyWord=" + keyWord + "&pageIndex=" + pageIndex + "&pageSize=" + pageSize;

            this.getData(apiUri, callback); //注意this
        };

        //获取所有角色信息（连同对应权限一起）
        ServiceProxy.prototype.getAllRoles = function (callback) {
            var authorID = 1;
            var isSuperAdmin = false;
            var apiUri = this._ServiceUri + CONST_PERMISSION_GETALLROLES_URI + "?UserID=" + authorID + "&IsSuperAdmin=" + isSuperAdmin;

            this.getData(apiUri, callback); //注意this
        };

        //获取所有角色项
        ServiceProxy.prototype.getAllRoleItems = function (callback) {
            var authorID = 1;
            var isSuperAdmin = false;
            var apiUri = this._ServiceUri + CONST_PERMISSION_GETALLROLEITEMS_URI + "?UserID=" + authorID + "&IsSuperAdmin=" + isSuperAdmin;

            this.getData(apiUri, callback); //注意this
        };

        //获取角色指定模块的权限
        ServiceProxy.prototype.getRoleAuthority = function (roleAuthorityID, callback) {
            var authorID = 1;
            var isSuperAdmin = false;
            var apiUri = this._ServiceUri + CONST_PERMISSION_GETROLEAUTHORITY_URI + "?UserID=" + authorID + "&IsSuperAdmin=" + isSuperAdmin + "&ObjectID=" + roleAuthorityID;

            this.getData(apiUri, callback); //注意this
        };

        //获取角色所有模块的权限
        ServiceProxy.prototype.getRoleAuthorities = function (roleID,callback) {
            var authorID = 1;
            var isSuperAdmin = false;
            var apiUri = this._ServiceUri + CONST_PERMISSION_GETROLEAUTHORITIES_URI + "?UserID=" + authorID + "&IsSuperAdmin=" + isSuperAdmin + "&ObjectID=" + roleID;

            this.getData(apiUri, callback); //注意this
        };

        //获取角色包含的用户
        ServiceProxy.prototype.getRoleUsers = function (roleID,userID,userName,pageIndex,pageSize, callback) {
            var authorID = 1;
            var isSuperAdmin = false;
            var apiUri = this._ServiceUri + CONST_PERMISSION_GETROLEUSERS_URI + "?authorID=" + authorID + "&isSuperAdmin=" + isSuperAdmin + "&roleID=" + roleID + "&userID=" + userID + "&userName=" + userName + "&pageSize=" + pageSize + "&pageIndex=" + pageIndex;

            this.getData(apiUri, callback); //注意this
        };

        //编辑角色（同其权限一起，集成添加功能）
        ServiceProxy.prototype.editRole = function (request, callback) {
            var apiUri = this._ServiceUri + CONST_PERMISSION_EDITROLE_URI;
            request.AuthorID = 1;
            request.IsSuperAdmin = false;
            this.postData(apiUri, request, callback); //注意this
        };

        //修改角色(单个模块)权限
        ServiceProxy.prototype.editRoleAuthority = function (request, callback) {
            var apiUri = this._ServiceUri + CONST_PERMISSION_EDITROLEAUTHORITY_URI;
            request.AuthorID = 1;
            request.IsSuperAdmin = false;
            this.postData(apiUri, request, callback); //注意this
        };

        //删除角色(单个模块)权限 
        ServiceProxy.prototype.removeRoleAuthority = function (roleAuthorityID, callback) {
            var apiUri = this._ServiceUri + CONST_PERMISSION_REMOVEROLEAUTHORITY_URI;
            var authorID = 1;
            var isSuperAdmin = false;
            var request = { "UserID": authorID, "IsSuperAdmin": isSuperAdmin, "ObjectID": roleAuthorityID }
            this.postData(apiUri, request, callback); //注意this
        };

        //删除角色
        ServiceProxy.prototype.removeRole = function (roleID, callback) {
            var apiUri = this._ServiceUri + CONST_PERMISSION_REMOVEROLE_URI;
            var authorID=1;
            var isSuperAdmin=false;
            var request={"UserID":authorID,"IsSuperAdmin":isSuperAdmin,"ObjectID":roleID}
            this.postData(apiUri,request, callback); //注意this
        };

        //给角色批量分配用户
        ServiceProxy.prototype.assignBatchUserToRole = function (request, callback) {
            var apiUri = this._ServiceUri + CONST_PERMISSION_ASSIGNBATCHUSERTOROLE_URI;
            request.AuthorID = 1;
            request.IsSuperAdmin = false;
            this.postData(apiUri, request, callback); //注意this
        };

        //将用户从角色中批量移除
        ServiceProxy.prototype.removeBatchUserFromRole = function (request, callback) {
            var apiUri = this._ServiceUri + CONST_PERMISSION_REMOVEBATCHUSERFROMROLE_URI;
            request.AuthorID = 1;
            request.IsSuperAdmin = false;
            this.postData(apiUri, request, callback); //注意this
        };
        
        //搜索用户信息
        ServiceProxy.prototype.searchUsers = function (request, callback) {
            var apiUri = this._ServiceUri + CONST_PERMISSION_SEARCHUSERS_URI;
            request.AuthorID = 1;
            request.IsSuperAdmin = false;
            this.postData(apiUri, request, callback); //注意this
        };

        //资讯
        //根据条件获取资讯文章
        ServiceProxy.prototype.getInfoTopics = function (cataID, mbID, pcaID, keyWord, pageIndex, pageSize, callback) {
            var apiUri = this._ServiceUri + CONST_INFO_GETINFOTOPICS_URI;
            //var request = { CataID: cataID, MBID: mbID, PCAID: pcaID,KeyWord: keyWord, PageQuery: { PageIndex: pageIndex, PageSize: pageSize } };
            //var request = { CataID: cataID, MBID: mbID, PCAID: pcaID,KeyWord: keyWord};
        

            var requestData = {};
            var page = {};
            page.PageIndex = pageIndex;
            page.PageSize = pageSize;

            requestData.CataID =cataID;
            requestData.MBID = mbID;
            requestData.PCAID = pcaID;
            requestData.KeyWord = keyWord;
            requestData.PageQuery = page

            //$.get(apiUri, { CataID: cataID }, callback, 'json');

            this.postData(apiUri, requestData, callback); //注意this
        };

        //获取资讯文章排行榜
        ServiceProxy.prototype.getInfoTopicRankings = function (cataID , num, callback) {
            var apiUri = this._ServiceUri + CONST_INFO_GETINFOTOPICRANKINGS_URI + "?CataID=" + cataID + "&Num=" + num;
            this.getData(apiUri, callback); //注意this
        };

        //获取最新资讯文章
        ServiceProxy.prototype.getNewestInfoTopics = function (num, callback) {
            var apiUri = this._ServiceUri + CONST_INFO_GETNEWESTINFOTOPICS_URI + "?Num=" + num ;
            this.getData(apiUri, callback); //注意this
        };

        //根据输入字母返回子集拼音
        ServiceProxy.prototype.getSubLettersByLetter = function (letter, callback) {
            var apiUri = this._ServiceUri + CONST_INFO_GETSUBLETTERSBYLETTER_URI + "?Letter=" + letter;
            this.getData(apiUri, callback); //注意this
        };

        //根据药材拼音获取药材信息
        ServiceProxy.prototype.getMCodexsByLetter = function (letter,num, callback) {
            var apiUri = this._ServiceUri + CONST_INFO_GETMCODEXSBYLETTER_URI + "?Letter=" + letter + "&MaxCount=" + num;
            this.getData(apiUri, callback); //注意this
        };

        //获取资讯文章详情
        ServiceProxy.prototype.getInfoTopicDetail = function (topicID,pageIndex, callback) {
            var apiUri = this._ServiceUri + CONST_INFO_GETINFOTOPICDETAIL_URI + "?TopicID=" + topicID + "&PageIndex=" + pageIndex;
            this.getData(apiUri, callback); //注意this
        };

        //获取资讯文章评论
        ServiceProxy.prototype.getInfoTopicReviews = function (topicID, num, callback) {
            var apiUri = this._ServiceUri + CONST_INFO_GETINFOTOPICREVIEWS_URI + "?TopicID=" + topicID + "&Num=" + num;
            this.getData(apiUri, callback); //注意this
        };

        //获取与药材相关的资讯文章基础信息
        ServiceProxy.prototype.getMBRelativedTopicBases = function (mbID, cataID, num, callback) {
            var apiUri = this._ServiceUri + CONST_INFO_GETMBRELATIVEDTOPICBASES_URI;
            var request = { MBID: mbID,CataID:cataID, Num: num };
            this.getDataWithRequestData(apiUri, request, callback); //注意this
        };

        //获取与药材相关的资讯文章项目
        ServiceProxy.prototype.getMBRelativedTopicItems = function (mbID, cataID, num, callback) {
            var apiUri = this._ServiceUri + CONST_INFO_GETMBRELATIVEDTOPICITEMS_URI;
            var request = { MBID: mbID, CataID: cataID, Num: num };
            this.getDataWithRequestData(apiUri, request, callback); //注意this
        };

        //获取排除指定栏目的药材相关的资讯文章
        ServiceProxy.prototype.getMBExcludeCataTopics = function (mbID, cataIDs, num, callback) {
            var apiUri = this._ServiceUri + CONST_INFO_GETMBEXCLUDECATATOPICS_URI;
            var request = { MBID: mbID, CataIDs: cataIDs, Num: num };
            this.getDataWithRequestData(apiUri, request, callback); //注意this
        };

        //获取与药材相关的资讯文章
        ServiceProxy.prototype.getMBRelativedTopics = function (MBIDs, num, callback) {
            var apiUri = this._ServiceUri + CONST_INFO_GETMBRELATIVEDTOPICS_URI;
            var request = { ObjectIDs: MBIDs, Num:num};
            this.getDataWithRequestData(apiUri, request, callback); //注意this
        };

        //获取与药材相关的帖子
        ServiceProxy.prototype.getMBRelativedPosts = function (MBNames,num, callback) {
            var apiUri = this._ServiceUri + CONST_INFO_GETMBRELATIVEDPOSTS_URI;
            var request = { ObjectNames: MBNames, Num: num };
            this.getDataWithRequestData(apiUri, request, callback); //注意this
        };

        //添加资讯文章评论
        ServiceProxy.prototype.addInfoTopicReview = function (topicID, personID,authorID, author,clientIP,title, content, callback) {
            var apiUri = this._ServiceUri + CONST_INFO_ADDINFOTOPICREVIEW_URI;
            var request = { TopicID: topicID,PersonID:personID, AuthorID: authorID, Author: author,AuthorIP:clientIP,Title:title, Content: content };
            this.postData(apiUri, request, callback); //注意this
        };

        //药材
        //获取(药材)品种基本信息
        ServiceProxy.prototype.getMCodexBase = function (mbID, callback) {
            var apiUri = this._ServiceUri + CONST_MCODEX_GETMCODEXBASE_URI + "?MBID=" + mbID;
            this.getData(apiUri, callback); //注意this
        };

        //获取药材的推荐商家
        ServiceProxy.prototype.getMCodexRecommendBusinesses = function (mbID, businessType, num, callback) {
            var apiUri = this._ServiceUri + CONST_MCODEX_GETMCODEXRECOMMENDBUSINESSES_URI;
            var request = { MBID: mbID, BusinessType: businessType, Num: num };
            this.getDataWithRequestData(apiUri, request, callback); //注意this
        };

        
        //价格
        //获取（药材）品种的价格信息
        ServiceProxy.prototype.getMCodexPrices = function (mbID,priceType,num, callback) {
            var apiUri = this._ServiceUri + CONST_PRICE_GETMCODEXPRICES_URI;
            var request = { MBID: mbID,PriceType:priceType, Num: num };
            this.getDataWithRequestData(apiUri, request, callback); //注意this
        };

        //验证校验码
        ServiceProxy.prototype.validateCode = function (code, callback, isAsync) {
            var apiUri = this._ServiceUri + CONST_VERIFYCODE_VALIDATECODE_URI + "?code=" + code;
            if (isAsync) {
                this.getData(apiUri, callback); //注意this
            }
            else {
                this.getDataSync(apiUri, callback); //注意this
            }

        };

        //添加收藏
        ServiceProxy.prototype.addFavoriteItem = function (userID, objTypeID, objID, callback) {
            var apiUri = this._ServiceUri + CONST_FAVOTITE_ADDFAVORITEITEM_URI;
            var request = { UserID: userID, ObjTypeID: objTypeID, ObjID: objID };
            this.postData(apiUri, request, callback); //注意this
        };

        //取消收藏
        ServiceProxy.prototype.removeFavoriteItem = function (userID, objTypeID, objID, callback) {
            var apiUri = this._ServiceUri + CONST_FAVOTITE_REMOVEFAVOTITE_URI;
            var request = { UserID: userID, ObjTypeID: objTypeID, ObjID: objID };
            this.postData(apiUri, request, callback); //注意this
        };

        //指定收藏项是否存在
        ServiceProxy.prototype.isExistFavoriteItem = function (userID, objTypeID, objID, callback) {
            var apiUri = this._ServiceUri + CONST_FAVOTITE_ISEXISTFAVORITEITEM_URI;
            var request = { UserID: userID, ObjTypeID: objTypeID, ObjID: objID };
            this.postData(apiUri, request, callback); //注意this
        };
        
        //请求数据
        ServiceProxy.prototype.getData = function (apiUri, callback) {
            $.ajax(
                      {
                          url: apiUri,
                          type: "GET",
                          dataType: "json",
                          success: callback
                      }
                 )
        };

        //请求数据同步
        ServiceProxy.prototype.getDataSync = function (apiUri, callback) {
            $.ajax(
                      {
                          async: false,
                          url: apiUri,
                          type: "GET",
                          dataType: "json",
                          success: callback
                      }
                 )
        };

       

        //请求数据带参数
        ServiceProxy.prototype.getDataWithRequestData = function (apiUri, requestData, callback) {
            $.ajax(
                      {
                          url: apiUri,
                          type: "GET",
                          data: requestData,
                          dataType: "json",
                          success: callback
                      }
                 )
        };

        //请求数据(Post方式）
        ServiceProxy.prototype.postData = function (apiUri,requestData, callback) {
            $.ajax(
                      {
                          url: apiUri,
                          type: "POST",
                          data:requestData,
                          dataType: "json",
                          success: callback
                      }
                 )
        };


        ServiceProxy._initialized = true;
    }
}